
import RedTipInfo from "./RedTipInfo";
import WidgetTipManager from "./WidgetTipManager";
import EventDispatcher from "../event/EventDispatcher";
import { RedTipPos, RedTipInfoType, RedTipEventName } from "./RedTipConst";
import ItemTipManager from "./ItemTipManager";
import TSMap from "../math/TSMap";
// import BaseView from "../base/view/BaseView";
// import BaseItemView from "../base/view/BaseItemView";
import RedTipAble from "../interface/RedTipAble";

export default class RedTipManager  extends EventDispatcher{
	private redInfoMap  = {}
    private layerMap = {}
    private widgetTipManager = null;
    private itemTipManager = null;

    private config:any;

    init(config:any,sf:cc.SpriteFrame){
        this.config = config;
        this.widgetTipManager = new WidgetTipManager(sf);
        this.itemTipManager = new ItemTipManager(sf);
        for (const redType in this.config) {
            if (this.config.hasOwnProperty(redType)) {
                const list:Array<any> = this.config[redType];
                for (let index = 0; index < list.length; index++) {
                    const element = list[index];
                    let lName = element[RedTipPos.layerName];
                    if(!this.layerMap[lName]){
                        this.layerMap[lName] = {};
                    }
                    if(! this.layerMap[lName][redType]){
                        this.layerMap[lName][redType] = [];
                    }
                    this.layerMap[lName][redType].push(element);
                }
            }
        } 
        // cc.log(" layerMap ",this.layerMap);           
    }
    constructor(){
        super();

    }

    isHaveLayer(layer:RedTipAble){
        let layerName =layer.getRedTipName()
        // cc.log("RedTipManager isHaveLayer ",layerName, this.layerMap[layerName]);
        if(this.layerMap[layerName]){
            return true;
        }
        return false;
    }

    private static ins:RedTipManager;
    static instance(){
        if(!this.ins){
            this.ins = new RedTipManager();
        }
        return this.ins;
    }

    /**
     * 添加对应红点类型的id
     * @param redType 红点类型 定义在RedTipType中
     * @param id 道具id。不是道具可以不添加。
     */
    add(redType,id = 999){
        if(! this.redInfoMap[redType]){
            this.redInfoMap[redType] = new RedTipInfo(redType);
        }
        // cc.log(" RedTipManager add redType id ",redType,id);
        if(this.redInfoMap[redType].add(id)){
            // this.widgetTipManager.addRedTypeInfo(redType,id);
            this.addRedTypeInfo(redType,id);
            if(this.redInfoMap[redType].getCount() == 1){
                this.publish(RedTipEventName.ADD_RED_TIP,redType,id);
            }
            this.publish(RedTipEventName.ADD_RED_TIP_ITEM,redType,id);
            
        }         
    }
    

    /**
     * 删除相关id
     * @param redType 
     * @param id 
     */
    delete(redType,id = 999){
        if(! this.redInfoMap[redType]){
           return;
        }     
        // cc.log("RedTipManager delete redType id ",redType,id);
        if(this.redInfoMap[redType].delete(id)){
            // cc.log("RedTipManager 2222222222222222 delete redType id ",redType,id);
            // this.widgetTipManager.deleteRedTypeInfo(redType,id);
            this.deleteRedTypeInfo(redType,id);
            // cc.log(' this.redInfoMap[redType].getCount() ',this.redInfoMap[redType].getCount())
            if(this.redInfoMap[redType].getCount() == 0){
                this.publish(RedTipEventName.DELETE_RED_TIP,redType,id);
            }
            this.publish(RedTipEventName.DELETE_RED_TIP_ITEM,redType,id);
        }          
    }

    /**
     * 
     * @param redType 
     */
    deleteAll(redType){
        let redTipInfo: RedTipInfo = this.redInfoMap[redType];
        if(redTipInfo){
            let data: TSMap = redTipInfo.getData();
            data.forEach((value,key)=>{
                this.deleteRedTypeInfo(redType,value);
            });
            redTipInfo.clear();
            this.publish(RedTipEventName.DELETE_RED_TIP,[redType]);
        }
    }
    /**
     * 
     * @param redType 
     * @param id 
     */
    private addRedTypeInfo(redType,id){
	    let config = this.config[redType]
        if( config ){ 
            for (let index = 0; index < config.length; index++) {
                const v = config[index];
                let layerName = v[RedTipPos.layerName];
                let widgetName = v[RedTipPos.widgetPath];
                if(! widgetName){
                    widgetName = layerName;
                }
                let tt = v[RedTipPos.redTipType]
                if( tt == RedTipInfoType.item){
                    this.itemTipManager.add(layerName,widgetName,redType,id);
                }else{
                    this.widgetTipManager.add(layerName,widgetName,redType,id);
                }
            }
        } 
    }
    

    /**
     * 
     * @param redType 
     * @param id 
     */
    private deleteRedTypeInfo(redType:string,id:number){
        let config = this.config[redType]
        if( config ){ 

            // cc.log('deleteRedTypeInfo  redType ',redType)
            for (let index = 0; index < config.length; index++) {
                const v = config[index];
                let layerName = v[RedTipPos.layerName];
                let widgetName = v[RedTipPos.widgetPath];
                if(! widgetName){
                    widgetName = layerName;
                }
                let tt:RedTipInfoType = v[RedTipPos.redTipType]
                if( tt == RedTipInfoType.item){
                    this.itemTipManager.delete(layerName,widgetName,redType,id)
                }else{
                    this.widgetTipManager.delete(layerName,widgetName,redType,id);
                }
            }
        }      
    }
    /**
     * BaseView 在start 时调用。
     * @param layer '
     */
    addWidgetRedTipByOnEnter( layer:RedTipAble ){
        let layerName = layer.getRedTipName()
        // cc.log("addWidgetRedTipByOnEnter layerName 11111111111111111",layerName);
        if(!this.layerMap[layerName]){
            // cc.log("addWidgetRedTipByOnEnter not have  ",layerName);
            return;
        }
        // cc.log("addWidgetRedTipByOnEnter layerName 2222222222222222222",layerName);
        for (const redType in this.redInfoMap) {
            const wList = this.layerMap[layerName][redType];
            if(wList){
                this.widgetTipManager.addRedTip(layer,wList);
            }
        }
    }

    /**
     * BaseView 收到添加通知会调用这个函数
     * @param layer 
     * @param data 
     */
    addWidgetRedTipByNotify( layer:RedTipAble,redType,id ){
        let layerName = layer.getRedTipName()
        // cc.log("addWidgetRedTipByNotify layerName ",layerName);
        if(!this.layerMap[layerName]){
            return;
        }
        // let redType = data[0];
        // let id = data[1];
        let wList = this.layerMap[layerName][redType];
        if(wList){
            this.widgetTipManager.addRedTip(layer,wList);
        }
    }

    /**
     * BaseView 在街道删除通知时调用
     * @param layer 
     * @param data 
     */
    deleteWidgetRedTipByNotify( layer:RedTipAble,redType,id?:number ){

        let layerName = layer.getRedTipName()
        // cc.log("deleteWidgetRedTipByNotify layerName ",layerName);
        if(!this.layerMap[layerName]){
            return;
        }
        // let redType = data[0];
        // let id = data[1];
        let wList = this.layerMap[layerName][redType];
        if(wList){
            this.widgetTipManager.deleteRedTip(layer,wList);
        }     
    }



    /**
     * BaseItemView在接到删除通知时条用。
     * @param layer 
     * @param data 
     * @param itemID 
     */
    deleteItemRedTipByNotify( layer:RedTipAble, redType,id:number,itemID:number ){
        let layerName = layer.getRedTipName()
        if(!this.layerMap[layerName]){
            return;
        }
        // let redType = data[0];
        // let id = data[1];
        if(!id){
            id = itemID;
        }
        if(itemID == id){
            let wList = this.layerMap[layerName][redType];
            if(wList){
                this.itemTipManager.deleteRedTip(layer,wList,id);
            }   
        }
    }
    
    /**
     * BaseItemView start调用
     * @param layer 
     * @param itemID 
     */
    addItemRedTipByOnEnter( layer: RedTipAble ,itemID){
        
        let layerName = layer.getRedTipName()
        // cc.log("addItemRedTipByOnEnter layerName ",layerName," itemID ",itemID);
        if(!this.layerMap[layerName]){
            return;
        }

        for (const redType in this.redInfoMap) {
            const redTipInfo:RedTipInfo = this.redInfoMap[redType];
            // cc.log("addItemRedTipByOnEnter redType ",redType);
            let wList = this.layerMap[layerName][redType];
            if(wList){
                let idMap:TSMap = redTipInfo.getData();
                // cc.log(" idMap size ",idMap.size());
                idMap.forEach((id)=>{
                    // cc.log(" id =================== ",id);
                    if(itemID == id){
                        this.itemTipManager.addRedTip(layer,wList,id);
                    }else{
                        this.itemTipManager.deleteOtherRedTip(layer,wList,itemID);
                    }
                })
                // for (let id in idMap) {

                // }
            }else{
                // cc.log("  wList is null ");
            }
        }

    }


    addItemRedTipByNotify( layer: RedTipAble,redType,id:number,itemID:number ){
        let layerName = layer.getRedTipName()
        // cc.log("addItemRedTipByNotify layerName ",layerName);
        if(!this.layerMap[layerName]){
            return;
        }    
        let wList = this.layerMap[layerName][redType];
        if(wList && itemID ){
            if(itemID == id){
                this.itemTipManager.addRedTip(layer,wList,id);
            }else{
                this.itemTipManager.deleteOtherRedTip(layer,wList,itemID);
            }            
        }          
    }

    print(){
        this.widgetTipManager.print();
    }
}
